﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Drawing;

public partial class giaovien_NhapDiem : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsThaoTac thaotac = new clsThaoTac();
    clsKiemTraTrangThai clstt = new clsKiemTraTrangThai();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                string magv = Session["Username"].ToString();
                cls.LoadComboboxNamHoc(ddlNamHoc);
                int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
                cls.LoadComboboxLopHoc(ddlLopHoc, manam);
                int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
                cls.LoadComboboxMonHocTheoMaGV(ddlMonHoc, malop, manam,magv);
                // cls.LoadComboboxLoaiDiem(ddlLoaiDiem);
                // tabDiemCuaLop.Focus();
                tabDiem.ActiveTabIndex = 0;
                LoadGridDiem();
            }
        }
    }
    void insert(int manam,int maky, int mamon)
    {
        for (int i = 17; i <= 46;i++ )
        {

            Diem dm = db.Diems.SingleOrDefault(p => p.MaNam == manam &&p.MaHS=="12021400"+i.ToString() && p.MaMon == mamon && p.MaKy == maky);
            dm.DiemMieng1 = 7;
            dm.Diem15_1 = 6;
            dm.Diem45_1 = 8;
            dm.DiemThi = 7;
            db.SubmitChanges();
        }


    }
    void LoadGrid(int maky)
    {
        // var dshsdacodiem=from p in db.Diems where p.
        var c = from p in db.Diems
                where
                     p.MaNam == int.Parse(ddlNamHoc.SelectedValue.ToString())
                    && p.MaMon == int.Parse(ddlMonHoc.SelectedValue.ToString())
                    && p.MaKy == maky
                select new
                {
                    p.MaHS,
                    p.HocSinh.TenHS,
                    p.DiemMieng1,
                    p.DiemMieng2,
                    p.DiemMieng3,
                    p.DiemMieng4,
                    p.DiemMieng5,
                    p.Diem45_1,
                    p.Diem45_2,
                    p.Diem45_3,
                    p.Diem45_4,
                    p.Diem15_1,
                    p.Diem15_2,
                    p.Diem15_3,
                    p.Diem15_4,
                    p.Diem15_5,
                    p.DiemThi
                };
        grvNhapDiem.DataSource = c;
        grvNhapDiem.DataBind();

    }
    protected void grvNhapDiem_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    void LoadGridDiem()
    {
        tabDiem.ActiveTabIndex = 0;
        manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        if (rdoKyI.Checked == true)
        {
            maky = 1;
            var c1 = db.proLoadDSHSChuaCoTrongBangDiem(mamon, maky, manam, malop);
           
            if (c1.Count() != 0)
            {


                var c = from p in db.proLoadDSHSChuaCoTrongBangDiem(mamon, maky, manam, malop)

                        select p.MaHS;
                foreach (var con in c)
                {
                    Diem dm = new Diem();
                    dm.MaNam = manam;
                    dm.MaKy = maky;
                    dm.MaMon = mamon;
                    dm.MaHS = con;
                    db.Diems.InsertOnSubmit(dm);
                    db.SubmitChanges();
                }
            }
            LoadDS(mamon, 1, manam, malop);
            lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);
            clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
            lblThongBao.Text = clstt.HienThiThongBao(manam, malop, mamon, maky);
        }
        if (rdoKyII.Checked == true)
        {
            maky = 2;
            var c2 = db.proLoadDSHSChuaCoTrongBangDiem(mamon, maky, manam, malop);
            
            if (c2.Count() != 0)
            {
                var c = from p in db.proLoadDSHSChuaCoTrongBangDiem(mamon, maky, manam, malop)

                        select p.MaHS;
                foreach (var con in c)
                {
                    Diem dm1 = new Diem();
                    dm1.MaNam = manam;
                    dm1.MaKy = 2;
                    dm1.MaMon = mamon;
                    dm1.MaHS = con;
                    db.Diems.InsertOnSubmit(dm1);
                    db.SubmitChanges();
                }
            }
            //LoadGrid(2);
            LoadDS(mamon, 2, manam, malop);
            lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);
            clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
        }
        kiemtratrangthai();
        AnTextTrenGrid(lblTrangThai.Text);
    }
    void kiemtratrangthai()
    {
        if (lblTrangThai.Text == "Đang chờ duyệt")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = true;

        }

        // grvNhapDiem.Enabled = true;
        if (lblTrangThai.Text == "Cần sửa lại bảng điểm")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;
        }

        if (lblTrangThai.Text == "Đã duyệt")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;

        }
        if (lblTrangThai.Text == "Cần sửa lại bảng điểm")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;

        }
        if (lblTrangThai.Text == "Đã sửa và đang chờ duyệt")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = true;

        }
        if (lblTrangThai.Text == "Chưa gửi bảng điểm")
        {
            btnLuuDiemCuaLop.Visible = true;
            btnGuiBangDiem.Visible= true;
            btnHuy.Visible = false;
            lblNgayGui.Text = "";

        }
        if (lblTrangThai.Text == "" || lblTrangThai.Text == null)
        {
            btnLuuDiemCuaLop.Visible = true;
        }
    }
    protected void ddlMonHoc_SelectedIndexChanged(object sender, EventArgs e)
    {

        LoadGridDiem();
    }
    protected void ddlNamHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        cls.LoadComboboxLopHoc(ddlLopHoc, manam);
        LoadGridDiem();
    }
    int manam = 0;
    int malop = 0;
    int mamon = 0;
    int maky = 0;
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {


    }
    void AnTextTrenGrid(string trangthai)
    {
        if (trangthai == "Đã duyệt")
        {
            for (int i = 0; i < grvNhapDiem.Rows.Count; i++)
            {

                for (int j = 1; j <= 5; j++)
                {
                    TextBox txtj = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng" + j.ToString());
                    txtj.Enabled = false;
                    TextBox txtj1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt15" + j.ToString());
                    txtj1.Enabled = false;

                }
                for (int j1 = 1; j1 <= 4; j1++)
                {
                    TextBox txtj2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt45" + j1.ToString());
                    txtj2.Enabled = false;
                }
                TextBox txtHK = (TextBox)grvNhapDiem.Rows[i].FindControl("txtHK");
                txtHK.Enabled = false;
            }
            btnLuuDiemCuaLop.Visible = true;
            btnHuy.Visible = true;
            btnGuiBangDiem.Visible = true;
        }
        if (trangthai != "Đã duyệt")
        {
            for (int i = 0; i < grvNhapDiem.Rows.Count; i++)
            {
                for (int j = 1; j <= 5; j++)
                {
                    TextBox txtj = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng" + j.ToString());
                    txtj.Enabled = true;
                    TextBox txtj1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt15" + j.ToString());
                    txtj1.Enabled = true;

                }
                for (int j1 = 1; j1 <5; j1++)
                {
                    TextBox txtj2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt45" + j1.ToString());
                    txtj2.Enabled = true;
                }
                TextBox txtHK = (TextBox)grvNhapDiem.Rows[i].FindControl("txtHK");
                txtHK.Enabled = true;
            }
        }

    }
    void LoadDS(int mamon, int maky, int manam, int malop)
    {
        grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
        grvNhapDiem.DataBind();
    }
    private void CapNhatDiem(TextBox text, out double diem)
    {
        diem = -1;
        if (text.Text != "")
        {
            diem = double.Parse(text.Text);
        }

    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {

        //lblGhiChu.Text = i.ToString();
    }
    private void NhapTuExcel(string filePath)
    {


        if (!System.IO.File.Exists(filePath))
        {
            return;
        }

        // Cắt đường dẫn tập tin để kiểm tra xem là xls hay xlsx
        string[] fileParts = filePath.Split('.');

        string connString = "";
        if (filePath.Length > 1 && fileParts[1] == "xls")// sử dụng cho Microsoft Excel 2003
        {
            connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";
        }
        else // sử dụng cho Microsoft Excel 2007
        {
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0";
        }

        // Tạo đối tượng kết nối
        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {
            // Mở kết nối
            oledbConn.Open();

            // Tạo đối tượng OleDBCommand và query data từ sheet có tên "Sheet1"
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);

            // Tạo đối tượng OleDbDataAdapter để thực thi việc query lấy dữ liệu từ tập tin excel
            OleDbDataAdapter oleda = new OleDbDataAdapter();

            oleda.SelectCommand = cmd;

            // Tạo đối tượng DataSet để hứng dữ liệu từ tập tin excel
            DataSet ds = new DataSet();

            // Đổ đữ liệu từ tập excel vào DataSet
            oleda.Fill(ds);
            DataTable dt = ds.Tables[0];
            DataTable diem = new DataTable();
            diem.Columns.Add("MaHS", typeof(string));
            diem.Columns.Add("TenHS", typeof(string));
            diem.Columns.Add("DiemMieng1", typeof(float));
            diem.Columns.Add("DiemMieng2", typeof(float));
            diem.Columns.Add("DiemMieng3", typeof(float));
            diem.Columns.Add("DiemMieng4", typeof(float));
            diem.Columns.Add("DiemMieng5", typeof(float));
            diem.Columns.Add("Diem15_1", typeof(float));
            diem.Columns.Add("Diem15_2", typeof(float));
            diem.Columns.Add("Diem15_3", typeof(float));
            diem.Columns.Add("Diem15_4", typeof(float));
            diem.Columns.Add("Diem15_5", typeof(float));
            diem.Columns.Add("Diem45_1", typeof(float));
            diem.Columns.Add("Diem45_2", typeof(float));
            diem.Columns.Add("Diem45_3", typeof(float));
            diem.Columns.Add("Diem45_4", typeof(float));
            diem.Columns.Add("DiemThi", typeof(float));

            //grvDiemExcel.DataSource = dt;
            //grvDiemExcel.DataBind();

            int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
            int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
            int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
            int maky = 0;
            if (rdoKyI.Checked == true) { maky = 1; }
            if (rdoKyII.Checked == true) { maky = 2; }
            int i;
            for (i = 0; i < dt.Rows.Count; i++)
            {
                //string ma = dt.Rows[i][1].ToString();
                //Response.Write(ma);
                DataRow row1 = diem.NewRow();
                //db.proCapNhatM1(manam, maky, mamon, ma, float.Parse(dt.Rows[i][3].ToString()));
                row1[0] = dt.Rows[i][1].ToString();
                row1[1] = dt.Rows[i][2].ToString();
                if (dt.Rows[i][1].ToString() != "")
                {
                    if (dt.Rows[i][3].ToString() != "")
                    {
                        row1[2] = float.Parse(dt.Rows[i][3].ToString());
                    }
                    if (dt.Rows[i][4].ToString() != "")
                    {
                        row1[3] = float.Parse(dt.Rows[i][4].ToString());

                    }
                    if (dt.Rows[i][5].ToString() != "")
                    {
                        row1[4] = float.Parse(dt.Rows[i][5].ToString());

                    }
                    if (dt.Rows[i][6].ToString() != "")
                    {
                        row1[5] = float.Parse(dt.Rows[i][6].ToString());

                    }
                    if (dt.Rows[i][7].ToString() != "")
                    {
                        row1[6] = float.Parse(dt.Rows[i][7].ToString());

                    }
                    if (dt.Rows[i][8].ToString() != "")
                    {
                        row1[7] = float.Parse(dt.Rows[i][8].ToString());
                    }
                    if (dt.Rows[i][9].ToString() != "")
                    {
                        row1[8] = float.Parse(dt.Rows[i][9].ToString());
                    }
                    if (dt.Rows[i][10].ToString() != "")
                    {
                        row1[9] = float.Parse(dt.Rows[i][10].ToString());

                    }
                    if (dt.Rows[i][11].ToString() != "")
                    {
                        row1[10] = float.Parse(dt.Rows[i][11].ToString());

                    }
                    if (dt.Rows[i][12].ToString() != "")
                    {
                        row1[11] = float.Parse(dt.Rows[i][12].ToString());

                    }

                    if (dt.Rows[i][13].ToString() != "")
                    {
                        row1[12] = float.Parse(dt.Rows[i][13].ToString());

                    }
                    if (dt.Rows[i][14].ToString() != "")
                    {
                        row1[13] = float.Parse(dt.Rows[i][14].ToString());

                    }
                    if (dt.Rows[i][15].ToString() != "")
                    {
                        row1[14] = float.Parse(dt.Rows[i][15].ToString());
                    }
                    if (dt.Rows[i][16].ToString() != "")
                    {
                        row1[15] = float.Parse(dt.Rows[i][16].ToString());

                    }


                    if (dt.Rows[i][17].ToString() != "")
                    {
                        row1[16] = float.Parse(dt.Rows[i][17].ToString());

                    }


                    diem.Rows.Add(row1);
                }

            }
            grvDiemExcel.DataSource = diem;
            grvDiemExcel.DataBind();
            ////lblGhiChu.Text=i.ToString();
            //grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
            //grvNhapDiem.DataBind();


        }
        catch
        {
        }
        finally
        {
            //Đóng chuỗi kết nối
            oledbConn.Close();
        }


    }
    private void NhapDiemTuExcel(int manam, int maky, int mamon, int malop)
    {
        for (int i = 0; i < grvDiemExcel.Rows.Count; i++)
        {
            Label lblMaHS = (Label)grvDiemExcel.Rows[i].FindControl("lblMaHS");
            TextBox txtM1 = (TextBox)grvDiemExcel.Rows[i].FindControl("txtMieng1");
            TextBox txtM2 = (TextBox)grvDiemExcel.Rows[i].FindControl("txtMieng2");
            TextBox txtM3 = (TextBox)grvDiemExcel.Rows[i].FindControl("txtMieng3");
            TextBox txtM4 = (TextBox)grvDiemExcel.Rows[i].FindControl("txtMieng4");
            TextBox txtM5 = (TextBox)grvDiemExcel.Rows[i].FindControl("txtMieng5");
            TextBox txt15_1 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt151");
            TextBox txt15_2 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt152");
            TextBox txt15_3 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt153");
            TextBox txt15_4 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt154");
            TextBox txt15_5 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt155");
            TextBox txt45_1 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt451");
            TextBox txt45_2 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt452");
            TextBox txt45_3 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt453");
            TextBox txt45_4 = (TextBox)grvDiemExcel.Rows[i].FindControl("txt454");
            TextBox txtHK = (TextBox)grvDiemExcel.Rows[i].FindControl("txtHK");
            var c = from p in db.Diems
                    where p.MaHS == lblMaHS.Text && p.MaNam == manam && p.MaKy == maky && p.MaMon == mamon
                    select p.MaHS;
            if (c.Count() == 0)
            {
                db.proThemDuLieu(manam, maky, mamon, lblMaHS.Text);
            }
            if (txtM1.Text != "")
            {
                db.proCapNhatM1(manam, maky, mamon, lblMaHS.Text, float.Parse(txtM1.Text));
            }
            if (txtM2.Text != "")
            {
                db.proCapNhatM2(manam, maky, mamon, lblMaHS.Text, float.Parse(txtM2.Text));
            }
            if (txtM3.Text != "")
            {
                db.proCapNhatM3(manam, maky, mamon, lblMaHS.Text, float.Parse(txtM3.Text));
            }
            if (txtM4.Text != "")
            {
                db.proCapNhatM4(manam, maky, mamon, lblMaHS.Text, float.Parse(txtM4.Text));
            }
            if (txtM5.Text != "")
            {
                db.proCapNhatM5(manam, maky, mamon, lblMaHS.Text, float.Parse(txtM5.Text));
            }
            if (txt15_1.Text != "")
            {
                db.proCapNhat15_1(manam, maky, mamon, lblMaHS.Text, float.Parse(txt15_1.Text));
            }
            if (txt15_2.Text != "")
            {
                db.proCapNhat15_2(manam, maky, mamon, lblMaHS.Text, float.Parse(txt15_2.Text));
            }
            if (txt15_3.Text != "")
            {
                db.proCapNhat15_3(manam, maky, mamon, lblMaHS.Text, float.Parse(txt15_3.Text));
            }
            if (txt15_4.Text != "")
            {
                db.proCapNhat15_4(manam, maky, mamon, lblMaHS.Text, float.Parse(txt15_4.Text));
            }
            if (txt15_5.Text != "")
            {
                db.proCapNhat15_5(manam, maky, mamon, lblMaHS.Text, float.Parse(txt15_5.Text));
            }
            if (txt45_1.Text != "")
            {
                db.proCapNhat45_1(manam, maky, mamon, lblMaHS.Text, float.Parse(txt45_1.Text));
            }
            if (txt45_2.Text != "")
            {
                db.proCapNhat45_2(manam, maky, mamon, lblMaHS.Text, float.Parse(txt45_2.Text));
            }
            if (txt45_3.Text != "")
            {
                db.proCapNhat45_3(manam, maky, mamon, lblMaHS.Text, float.Parse(txt45_3.Text));
            }
            if (txt45_4.Text != "")
            {
                db.proCapNhat45_4(manam, maky, mamon, lblMaHS.Text, float.Parse(txt45_4.Text));
            }
            if (txtHK.Text != "")
            {
                db.proCapNhatDiemThi(manam, maky, mamon, lblMaHS.Text, float.Parse(txtHK.Text));
            }
            db.proCapNhatDTBMon(manam, maky, mamon, lblMaHS.Text, db.funcReturnTKDiem(lblMaHS.Text, mamon, maky, manam));
            //}
        }

    }
    protected void btnNhapExcel_Click(object sender, EventArgs e)
    {
        if (fuNhapExcel.HasFile)
        {

            string path = Server.MapPath("..\\upload") + "\\" + fuNhapExcel.FileName;
            fuNhapExcel.SaveAs(path);
            NhapTuExcel(path);
            //tabDiemTheoExcel.Dispose();
            //tabDiemTheoExcel.TabIndex = 0;
            lblNhapExcel.InnerText = "";
            tabDiem.ActiveTabIndex = 1;

        }
        else if (!fuNhapExcel.HasFile)
        {
            lblNhapExcel.InnerText = "Bạn phải chọn đường dẫn!";
        }
    }
    protected void btnLuuExcel_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        {
            maky = 1;
        }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        NhapDiemTuExcel(manam, maky, mamon, malop);
        grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
        grvNhapDiem.DataBind();
        CapNhatDiemTongKetCuaHS(manam, maky, mamon);
        tabDiemCuaLop.Focus();


    }
    //private void ThayDoiMauChu()
    // {
    //     int i;
    //     for (i = 0; i < grvNhapDiem.Rows.Count; i++)
    //     {
    //         Label lblMaHS = (Label)grvNhapDiem.Rows[i].FindControl("lblMaHS");
    //         TextBox txtM1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng1");
    //         TextBox txtM2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng2");
    //         TextBox txtM3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng3");
    //         TextBox txtM4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng4");
    //         TextBox txtM5 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng5");
    //         TextBox txt15_1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt151");
    //         TextBox txt15_2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt152");
    //         TextBox txt15_3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt153");
    //         TextBox txt15_4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt154");
    //         TextBox txt15_5 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt155");
    //         TextBox txt45_1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt451");
    //         TextBox txt45_2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt452");
    //         TextBox txt45_3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt453");
    //         TextBox txt45_4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt454");
    //         TextBox txtHK = (TextBox)grvNhapDiem.Rows[i].FindControl("txtHK");
    //         if (txtM1.)
    //         {
    //             txtM1.ForeColor = Color.Yellow;
    //         }
    //     }


    protected void btnLuuDiemCuaLop_Click(object sender, EventArgs e)
    {
        btnGuiBangDiem.Enabled = true;
        manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        int i;
        for (i = 0; i < grvNhapDiem.Rows.Count; i++)
        {
            Label lblMaHS = (Label)grvNhapDiem.Rows[i].FindControl("lblMaHS");
            TextBox txtM1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng1");
            TextBox txtM2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng2");
            TextBox txtM3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng3");
            TextBox txtM4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng4");
            TextBox txtM5 = (TextBox)grvNhapDiem.Rows[i].FindControl("txtMieng5");
            TextBox txt15_1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt151");
            TextBox txt15_2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt152");
            TextBox txt15_3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt153");
            TextBox txt15_4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt154");
            TextBox txt15_5 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt155");
            TextBox txt45_1 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt451");
            TextBox txt45_2 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt452");
            TextBox txt45_3 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt453");
            TextBox txt45_4 = (TextBox)grvNhapDiem.Rows[i].FindControl("txt454");
            TextBox txtHK = (TextBox)grvNhapDiem.Rows[i].FindControl("txtHK");

            Diem dm = db.Diems.SingleOrDefault(p => p.MaHS == lblMaHS.Text && p.MaKy == maky && p.MaNam == manam && p.MaMon == mamon);
            //diem mieng
            if (txtM1.Text != "")
            { dm.DiemMieng1 = float.Parse(txtM1.Text); }
            if (txtM1.Text == "")
            { dm.DiemMieng1 = null; }
            if (txtM2.Text != "")
            { dm.DiemMieng2 = float.Parse(txtM2.Text); }
            if (txtM2.Text == "")
            { dm.DiemMieng2 = null; }
            if (txtM3.Text != "")
            { dm.DiemMieng3 = float.Parse(txtM3.Text); }
            if (txtM3.Text == "")
            { dm.DiemMieng3 = null; }
            if (txtM4.Text != "")
            { dm.DiemMieng4 = float.Parse(txtM4.Text); }
            if (txtM4.Text == "")
            { dm.DiemMieng4 = null; }
            if (txtM5.Text != "")
            { dm.DiemMieng5 = float.Parse(txtM5.Text); }
            if (txtM5.Text == "")
            { dm.DiemMieng5 = null; }
            //diem 15 phut
            if (txt15_1.Text != "")
            { dm.Diem15_1 = float.Parse(txt15_1.Text); }
            if (txt15_1.Text == "")
            { dm.Diem15_1 = null; }
            if (txt15_2.Text != "")
            { dm.Diem15_2 = float.Parse(txt15_2.Text); }
            if (txt15_2.Text == "")
            { dm.Diem15_2 = null; }
            if (txt15_3.Text != "")
            { dm.Diem15_3 = float.Parse(txt15_3.Text); }
            if (txt15_3.Text == "")
            { dm.Diem15_3 = null; }
            if (txt15_4.Text != "")
            {
                dm.Diem15_4 = float.Parse(txt15_4.Text);
            }
            if (txt15_4.Text == "")
            { dm.Diem15_4 = null; }
            if (txt15_5.Text != "")
            { dm.Diem15_5 = float.Parse(txt15_5.Text); }
            if (txt15_5.Text == "")
            { dm.Diem15_5 = null; }
            // điểm 45 phut
            if (txt45_1.Text != "")
            { dm.Diem45_1 = float.Parse(txt45_1.Text); }
            if (txt45_1.Text == "")
            { dm.Diem45_1 = null; }
            if (txt45_2.Text != "")
            {
                dm.Diem45_2 = float.Parse(txt45_2.Text);
            }
            if (txt45_2.Text == "")
            { dm.Diem45_2 = null; }
            if (txt45_3.Text != "")
            { dm.Diem45_3 = float.Parse(txt45_3.Text); }
            if (txt45_3.Text == "")
            { dm.Diem45_3 = null; }
            if (txt45_4.Text != "")
            { dm.Diem45_4 = float.Parse(txt45_4.Text); }
            if (txt45_4.Text == "")
            { dm.Diem45_4 = null; }
            //diem thi
            if (txtHK.Text != "")
            { dm.DiemThi = float.Parse(txtHK.Text); }
            if (txtHK.Text == "")
            { dm.DiemThi = null; }
            db.SubmitChanges();
            db.proCapNhatDTBMon(manam, maky, mamon, lblMaHS.Text, db.funcReturnTKDiem(lblMaHS.Text, mamon, maky, manam));
        }
        grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
        grvNhapDiem.DataBind();
        CapNhatDiemTongKetCuaHS(manam, maky, malop);
        //TacVus tv = new TacVus();
        string tentv = "Cập nhật điểm";
        string tengv = Session["Username"].ToString();
        string noidung = "Đã cập nhật điểm của lớp" + ddlLopHoc.SelectedItem.Text + " " + "cho môn" + " " + ddlMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVu(tentv, noidung, DateTime.Now, tengv);
        kiemtratrangthai();
        AnTextTrenGrid(lblTrangThai.Text);

    }
    private double TinhDiemTongKetCuaHS(int manam, int maky, int malop, string mahs)
    {
        var c = from p in db.funcLayDTBTatCaCacMonCuaHocsinhHu(manam, malop, maky, mahs)
                select new { p.MaHS, p.MaMon, p.DTBMonHocKy, p.HeSoMon };
        int heso = 0;
        float tongdiemtb = 0;
        double diemTBC = 0;
        foreach (var con in c)
        {
            if (con.DTBMonHocKy != null)
            {
                tongdiemtb = tongdiemtb + float.Parse(con.DTBMonHocKy.ToString()) * int.Parse(con.HeSoMon.ToString());
                heso = heso + int.Parse(con.HeSoMon.ToString());
                string tmp = (tongdiemtb / heso).ToString();
                string diem = LamTronDiem(tmp);
                diemTBC = double.Parse(diem);

            }
        }

        return diemTBC;
    }
    public string LamTronDiem(string diem)
    {
        string kq = "";
        char[] kitu = diem.ToCharArray();
        if (kitu.Length <= 3)
        {

            kq = diem;
        }
        if (kitu.Length >= 4)
        {
            string con = diem.Substring(0, 4);
            int d = int.Parse(con.Substring(3));
            if (d >= 5 && d <= 9)
            {

                kq = (double.Parse(diem.Substring(0, 3)) + 0.1).ToString();
            }
            if (d < 5 && d >= 0)
            {
                kq = diem.Substring(0, 3);
            }

        }
        char[] kt = kq.ToCharArray();
        if (kt.Length == 1)
        { kq = kq + ".0"; }

        return kq;
    }
    private void CapNhatDiemTongKetCuaHS(int manam, int maky, int malop)
    {
        var c =from p in db.funcTraVeMaHSTheoLop(manam, maky, malop) select p.MaHS;

        foreach (var con in c)
        {
            var c1 = from p in db.KetQuaTongHops where p.MaNam == manam && p.MaKy == maky && p.MaHS == con select p;
            if (c1.Count() != 0)
            {
                double dtk = TinhDiemTongKetCuaHS(manam, maky, malop, con);
                KetQuaTongHop kqth = db.KetQuaTongHops.SingleOrDefault(p => p.MaNam == manam && p.MaKy == maky && p.MaHS == con);
                //kqth.DiemTongKet = TinhDiemTongKetCuaHS(manam, maky, malop, con);
                kqth.DiemTongKet = dtk;
                db.SubmitChanges();
            }
            if (c1.Count() == 0)
            {
                KetQuaTongHop kqth = new KetQuaTongHop();
                kqth.MaNam = manam;
                kqth.MaKy = maky;
                kqth.MaHS = con;
                kqth.DiemTongKet = TinhDiemTongKetCuaHS(manam, maky, malop, con);
                db.KetQuaTongHops.InsertOnSubmit(kqth);
                db.SubmitChanges();
            }
        }

    }
    private void DoiMauChuChoDiemKem()
    {

    }
    protected void grvNhapDiem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 1; i <= 5; i++)
            {
                TextBox txtMieng = (TextBox)e.Row.FindControl("txtMieng" + i.ToString());
                KiemTraDiemKem(txtMieng);
            }
            for (int i = 1; i <= 5; i++)
            {
                TextBox txt15 = (TextBox)e.Row.FindControl("txt15" + i.ToString());
                KiemTraDiemKem(txt15);
            }
            for (int i = 1; i <= 4; i++)
            {
                TextBox txt45 = (TextBox)e.Row.FindControl("txt45" + i.ToString());
                KiemTraDiemKem(txt45);
            }
            TextBox txtHK = (TextBox)e.Row.FindControl("txtHK");
            KiemTraDiemKem(txtHK);

        }
    }
    void KiemTraDiemKem(TextBox txt)
    {
        if (txt.Text != "")
        {
            float d = float.Parse(txt.Text);
            if (d < 3.5)
            {
                txt.ForeColor = Color.Red;
            }
        }
    }

    protected void btnGuiBangDiem_Click(object sender, EventArgs e)
    {

        int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyII.Checked == true)
        { maky = 2; }

        if (clstt.kiemtra(manam, malop, mamon, maky) == false)
        {

            TrangThaiBangDiem ttbd = new TrangThaiBangDiem();
            ttbd.MaNam = manam;
            ttbd.MaMon = mamon;
            ttbd.MaLop = malop;
            ttbd.MaKy = maky;
            ttbd.TrangThaiChoDuyet = true;
            ttbd.TrangThaiHoanThanh = false;
            ttbd.ChuaSua = false;
            ttbd.DaSua = false;
            ttbd.NgayGuiBD = DateTime.Now.Date; 
            db.TrangThaiBangDiems.InsertOnSubmit(ttbd);
            db.SubmitChanges();
            lblTrangThai.Text = "Đang chờ duyệt";
            clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);


        }
        if (clstt.kiemtra(manam, malop, mamon, maky) == true)
        {
            if (clstt.TTHoanThanh(manam, malop, mamon, maky) == false)
            {
                if (clstt.TTsua(manam, malop, mamon, maky) == false)
                {
                    TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
                    ttbd.TrangThaiChoDuyet = true;
                    ttbd.TrangThaiHoanThanh = false;
                    //ttbd.NgayGuiBD = DateTime.Now.Date;
                    db.SubmitChanges();
                    lblTrangThai.Text = "Đang chờ duyệt";
                    clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
                }
                if (clstt.TTsua(manam, malop, mamon, maky) == true)
                {
                    TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
                    ttbd.TrangThaiChoDuyet = true;
                    ttbd.TrangThaiHoanThanh = false;
                    ttbd.DaSua = true;
                    ttbd.NgayGuiBD = DateTime.Now.Date;
                    db.SubmitChanges();
                    lblTrangThai.Text = "Đã sửa và đang chờ duyệt";
                    clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
                }
            }

        }
        kiemtratrangthai();
        AnTextTrenGrid(lblTrangThai.Text);
        string tentv = "Gửi bảng điểm";
        string tengv = Session["Username"].ToString();
        string noidung = "Đã gửi bảng điểm lớp" + ddlLopHoc.SelectedItem.Text + " " + "của môn" + " " + ddlMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVu(tentv, noidung, DateTime.Now, tengv);
    }
    //bool kiemtra(int manam,int malop,int mamon,int maky)
    //{
    //    bool kt=true;
    //    var c = from p in db.TrangThaiBangDiems
    //            where p.MaKy == maky && p.MaLop == malop && p.MaMon == mamon && p.MaNam == manam
    //            select new { p.MaKy, p.MaMon, p.MaLop, p.MaNam };
    //    if (c.Count() == 0)
    //    {
    //        kt = false;
    //    }
    //    if (c.Count() != 0)
    //    { kt = true; }
    //    return kt;
    //}
    //bool TTHoanThanh(int manam, int malop,int mamon, int maky)
    //{
    //    bool tt = true;
    //    var c = from p in db.TrangThaiBangDiems
    //            where p.MaKy == maky && p.MaLop == malop && p.MaMon == mamon && p.MaNam == manam
    //            select p.TrangThaiHoanThanh;
    //    if (c.Count() != 0)
    //    {
    //        if (c.First() == null || c.First() == false)
    //        {
    //            tt = false;
    //        }
    //        if (c.First() == true)
    //        {
    //            tt = true;
    //        }
    //    }
    //    return tt;
    //}
    //string trangthai(int manam, int malop, int mamon, int maky)
    //{
    //    string tt1 = "";
    //    var c = from p in db.TrangThaiBangDiems
    //            where p.MaKy == maky && p.MaLop == malop && p.MaMon == mamon && p.MaNam == manam
    //            select new{p.TrangThaiHoanThanh,p.TrangThaiChoDuyet};
    //    if (c.Count() != 0)
    //    {
    //        if (c.First().TrangThaiHoanThanh == true)
    //        {
    //            tt1 = "Đã duyệt";

    //        }
    //        if (c.First().TrangThaiHoanThanh == false)
    //        {
    //            if (c.First().TrangThaiChoDuyet == true)
    //            {
    //                tt1 = "Đang chờ duyệt";

    //            }
    //        }
    //        if (c.First().TrangThaiHoanThanh == false)
    //        {
    //            if (c.First().TrangThaiChoDuyet == false)
    //            {
    //                tt1 = "Chưa gửi bảng điểm";

    //            }
    //        }
    //    }
    //    if (c.Count()==0)
    //    {
    //        tt1 = "Chưa gửi bảng điểm";
    //    }
    //    return tt1;

    //}

    protected void btnHuy_Click(object sender, EventArgs e)
    {
        int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyII.Checked == true)
        { maky = 2; }
        if (lblTrangThai.Text == "Đang chờ duyệt")
        {
            TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
            ttbd.TrangThaiChoDuyet = false;
            db.SubmitChanges();
        }
        lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);
        kiemtratrangthai();
        AnTextTrenGrid(lblTrangThai.Text);
        string tentv = "Hủy gửi bảng điểm";
        string tengv = Session["Username"].ToString();
        string noidung = "Đã hủy gửi bảng điểm lớp" + ddlLopHoc.SelectedItem.Text + " " + "của môn" + " " + ddlMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVu(tentv, noidung, DateTime.Now, tengv);
    }
    void ChanThaoTacNhapDiem()
    {
        //grvNhapDiem.Enabled = false;
    }
    protected void grvNhapDiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grvNhapDiem.PageIndex = e.NewPageIndex;
        int malop = int.Parse(ddlLopHoc.SelectedValue.ToString());
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        this.LoadDS(mamon, maky, manam, malop);
    }
    protected void rdoKyII_CheckedChanged(object sender, EventArgs e)
    {
       
        LoadGridDiem();
        btnLuuDiemCuaLop.Visible = true;
    }
    protected void rdoKyI_CheckedChanged(object sender, EventArgs e)
    {
        
        LoadGridDiem();
        btnLuuDiemCuaLop.Visible = true;
    }
    protected void ddlLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        LoadGridDiem();
        btnLuuDiemCuaLop.Visible = true;
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(ddlNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(ddlMonHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyII.Checked == true)
        { maky = 2; }
        insert(manam, 2, maky);
    }
}
